//
//  ItemsViewController.m
//  Dsgfds
//
//  Created by lanou on 15/10/27.
//  Copyright © 2015年 胡泉. All rights reserved.
//

#import "ItemsViewController.h"
#import "PKRequestManager.h"
#import "ItemsModel.h"
#import "ItemsTableViewCell.h"
#import "ItemsDetailViewController.h"
#import "FMDatabase.h"
#import "MJRefresh.h"
@interface ItemsViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) FMDatabase *dataBase;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSMutableDictionary *dataDic;
@property (nonatomic, strong) UITableView *mainListTableView;
@property (nonatomic, assign) BOOL isLoad;
@end

@implementation ItemsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.mainListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:(UITableViewStyleGrouped)];
    _mainListTableView.rowHeight = 352 * SCREEN_WIDTH / 630;
    _mainListTableView.delegate = self;
    _mainListTableView.dataSource = self;
    self.dataArray = [NSMutableArray array];
    self.dataDic = [NSMutableDictionary dictionary];
    UINib *nib = [UINib nibWithNibName:@"ItemsTableViewCell" bundle:nil];
    [_mainListTableView registerNib:nib forCellReuseIdentifier:@"Icell"];
    [self.view addSubview:_mainListTableView];
    _isLoad = NO;
    [self configureData];
    
    
    ItemsViewController *item = self;
    _mainListTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        _isLoad = YES;
        [item configureData];
    }];
    [_mainListTableView.header beginRefreshing];
    
    
    // 更改状态栏颜色
//    [self preferredStatusBarStyle];
    // Do any additional setup after loading the view.
}

//-(UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (void)configureData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"cat_id"] = _urlId;
    [PKRequestManager PKRequestManagerrequsetWithType:POST URLString:@"http://app.iliangcang.com/topic/listinfo?app_key=iphone&build=150&osVersion=83&v=2.2.6" ParDIc:parDic finish:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        
        if (_isLoad)
        {
            [_dataArray removeAllObjects];
        }
        for (NSString *str in dict[@"data"][@"keys"])
        {
            [_dataArray addObject:str];
            NSMutableArray *array = [NSMutableArray array];
            for (NSDictionary *inDic in dict[@"data"][@"infos"][str]) {
                ItemsModel *model = [[ItemsModel alloc] init];
                [model setValuesForKeysWithDictionary:inDic];
                if (self.navigationItem.title == nil)
                {
                    self.navigationItem.title = model.cat_name;
                    
                }
                [array addObject:model];
            }
            [_dataDic setObject:array forKey:str];
        }
        [self.mainListTableView reloadData];
        [_mainListTableView.header endRefreshing];
    } error:^(NSError *error) {
        
    }];
    
    
    
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[_dataDic objectForKey:_dataArray[section]] count];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ItemsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Icell" forIndexPath:indexPath];
    ItemsModel *model = [_dataDic valueForKey:_dataArray[indexPath.section]][indexPath.row];
    [cell passValueOfModel:model];
    // Configure the cell...
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 30;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *secView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    secView.backgroundColor = [UIColor colorWithWhite:0.272 alpha:1.000];
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 30)];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.font = [UIFont systemFontOfSize:12];
    textLabel.textColor = [UIColor whiteColor];
    textLabel.text = _dataArray[section];
    [secView addSubview:textLabel];
    return secView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ItemsDetailViewController *itemsDVC = [[ItemsDetailViewController alloc] init];
    itemsDVC.model = [_dataDic valueForKey:_dataArray[indexPath.section]][indexPath.row];
    [self.navigationController pushViewController:itemsDVC animated:YES];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
